<template>
	<widget-root :styles="styles">
		<goods :content="content" :styles="styles" @buy="openGoodsSpec" />
        <!-- 规格选择Popup -->
        <goods-spec
            v-model="showGoodsSpec"
            :defaultInfo="{
                image: goodsInfo.image,
                price: goodsInfo.sell_price,
                stock: goodsInfo.total_stock,
                unit: goodsInfo.unit_name,
        		member_level: goodsInfo.member_level,
                limit_type: goodsInfo.limit_type,
                limit_value: goodsInfo.limit_value,
                buy_num: goodsInfo.buy_num,
                cart_goods_num: goodsInfo.cart_goods_num,
        		isDividendPoolStatus: isDividendPoolStatus,
        		firstSpecMinNum: firstSpecInfo.min_num
            }"
            :buttons="specButtonsList"
            :spec-list="goodsInfo.spec_value"
            :spec-map="goodsInfo.spec_value_list"
            @buy="handleBuy"
            @cart="handleCartAdd"
            @change="changeGoodsSpec"
        />
        <!-- E 规格 -->
	</widget-root>
</template>

<script type="text/javascript">
    import { apiGoodsDetail } from '@/api/goods'
    import { OrderTypeEnum } from '@/utils/enum'
	import goods from './goods.vue'
	export default {
		components: {
			goods
		},
		props: {
			content: {
				type: [Object, Array]
			},
			styles: {
				type: [Object, Array]
			},
		},
		data() {
			return {
                goodsPopLoading: false,
                showGoodsSpec: false,
                goodsInfo: {},
                addressId: '',
                specButtonsList: [{
                    event: 'buy',
                    text: '立即购买'
                }]
                
            }
		},
		
		computed: {

			goods() {
				const {
					data
				} = this.content
				return data.length ? data : []
			},
			btnStyle() {
				const {
					btn_bg_color,
					btn_color,
					btn_border_radius,
					btn_border_color
				} =
				this.styles
				const {
					btn_bg_type
				} = this.content
				const style = {
					"background-color": btn_bg_color,
					color: btn_color,
					'border-radius': `${this.$px2rpx(btn_border_radius)}rpx`,
					'border-color': btn_border_color,
				}
				if (btn_bg_type == 2 || btn_bg_type === 5) delete style['background-color']
				return style
			},
            // 是否分公司 只有分公司，才有合伙人的设置功能。
            isDividendPoolStatus() {
            	if (this.userInfo && this.userInfo.dividend_pool_status) {
            		return this.userInfo.dividend_pool_status === 1
            	}
            	return false
            },
            specValueList() {
            	if (this.goodsInfo && this.goodsInfo.spec_value_list) {
            		return this.goodsInfo.spec_value_list || []
            	}
            	return []
            },
            firstSpecInfo() {
            	if (this.specValueList && this.specValueList.length) {
            		return this.specValueList[0]
            	}
            	return {}
            }
		},
        methods: {
        	navigate(item) {
        		this.$Router.push({
        			path: '/pages/goods_detail/goods_detail',
        			query: {
        				id: item.id
        			}
        		})
        	},
            // 初始化商品详情数据
            initGoodsDetail(item) {
                if (this.goodsPopLoading) {
                    return
                }
                this.goodsPopLoading = true
                // uni.showLoading({
                //     title: 'loading……'
                // })
                uni.showLoading();
                apiGoodsDetail({
                    id: item.id,
                    visit: 1,
                })
                    .then((data) => {
                        this.goodsInfo = data || {}
                        this.addressId = data.address?.id
                        if (data && Object.keys(data).length) {
                            this.showGoodsSpec = true
                        }
                    })
                    .catch((err) => {
                       
                    }).finally(() => {
                        this.goodsPopLoading = false
                        uni.hideLoading()
                    })
            },
            openGoodsSpec(item) {
                this.initGoodsDetail(item)
            },
            // 处理 立即购买
            handleBuy({ spec, number }) {
                const form = this.goodsInfo.type == 1 ? 'GOODS' : 'VIRTUAL'
                this.$Router.push({
                    path: '/pages/goods_order/goods_order',
                    query: {
                        from: OrderTypeEnum[form],
                        address_id: this.addressId,
                        payload: {
                            source: 'buy_now',
                            goods: [
                                {
                                    goods_num: number,
                                    item_id: spec.id
                                }
                            ]
                        }
                    }
                })
            },
            handleCartAdd() {
                
            },
            changeGoodsSpec(){
                
            }
        },
	};
</script>
<style lang="scss" scoped>

</style>
